/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.uitls;

import ac.lib.database.operations;
import ac.settings.dataModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

/**
 *
 * @author empcl_000
 */
public class data implements dataModel {

    propertiesOperation po = new propertiesOperation();
    operations db = new operations();

    public boolean withOutData = false;

    //<editor-fold defaultstate="collapsed" desc="Index methoeds">
    public void IndexClear(JTable index) {
    }

    public String IndexGetText(JTable index) {
        String returnData = "";
        int sel = index.getSelectedRow();
        if (sel != -1) {
            returnData = index.getValueAt(sel, 1).toString();
        }
        return returnData;

    }

    public String IndexGetValue(JTable index) {
        String returnData = "";
        try {

            int sel = index.getSelectedRow();
            if (sel >= 0) {
                returnData = index.getValueAt(sel, 0).toString();
            }

        } catch (Exception e) {
            //  ac.ui.utils.sendError(index, "X", e.getMessage());
        }
        return returnData;
    }

    public void IndexsetValue(JTable index, String val) {
        String returnData = "";

        for (int i = 0; i < index.getRowCount(); i++) {

            returnData = index.getValueAt(i, 0).toString();
            if (returnData.equals(val)) {
                index.getSelectionModel().setSelectionInterval(i, i);
            }

        }

    }

    public void tableClearCols(JTable index) {
        for (int r = 0; r < index.getColumnCount(); r++) {
            index.getColumnModel().removeColumn(index.getColumnModel().getColumn(r));

        }

    }

    public String updateCatid(int catid) {

        String[] data = db.getRowWithOutCon(tableAddstree, catid);
        return data[1];

    }

    public String[] updateArrayLang(String[] fields) {
        String[] r = new String[fields.length];

        for (int i = 0; i < fields.length; i++) {

            r[i] = po.getWord("label." + fields[i]);

        }
        return r;
    }

    public void Indexupdate(JTable index, String Tabel, String[] fields, String where, String[] viewDataAS) {
        String d[] = new String[fields.length + 1];
        d[0] = "id";

        for (int i = 1; i <= fields.length; i++) {
            d[i] = fields[i - 1];

        }

        fields = d;

        tableClearCols(index);
        DefaultTableModel model = (DefaultTableModel) index.getModel();

        model.setRowCount(0);

        model.setColumnIdentifiers(updateArrayLang(fields));
        db.setOpenCon();
        String w = "";
        if (!where.equals("")) {
            w = where + " and ";
        }

        if (withOutData) {

            w += " 1=1";
        } else {
            w += "  `rowstatus`='0' ";

        }

        ResultSet rs = db.getDataWithOutCon(Tabel, "", w + " ORDER BY id  DESC");
        String test = db.retuenErrorMessage;
        test += db.retuensqlString;
        try {

            if (viewDataAS == null) {

                while (rs.next()) {

                    Object[] n = new Object[fields.length];
                    for (int s = 0; s < fields.length; s++) {
                        alignrender(index, s);
                        String g = rs.getString(fields[s]);

                        n[s] = g;

                    }
                    model.addRow(n);

                }
            } else {

                while (rs.next()) {

                    Object[] n = new Object[fields.length];
                    for (int s = 0; s < fields.length; s++) {

                        String g = rs.getString(fields[s]);

                        if (fields[s].equalsIgnoreCase(CatID)) {

                            g = updateCatid(Integer.parseInt(g));
                        }

                        if (viewDataAS != null) {
                            String[] ss = viewDataAS[s].split("_");

                            if (ss.length > 0) {
                                if (!ss[0].equalsIgnoreCase("")) {

                                    switch (ss[0]) {

                                        case "clientType":
                                            if (g.equals("c")) {
                                                n[s] = po.getWord("label.cus");
                                            } else if (g.equals("s")) {
                                                n[s] = po.getWord("label.sup");
                                            } else if (g.equals("a")) {
                                                n[s] = po.getWord("label.cus&sup");
                                            }

                                            break;

                                        case "treasuryOType":
                                            if (g.equals("w")) {
                                                n[s] = po.getWord("label.withdraw");
                                            } else if (g.equals("d")) {
                                                n[s] = po.getWord("label.Deposit");
                                            } else {
                                                n[s] = g;
                                            }

                                            break;

                                        case "productID":
                                        case "productName":
                                            if (!g.equals("")) {
                                                String[] p = db.getRow(TABLE_products, Integer.parseInt(g));

                                                n[s] = p[1];
                                            }
                                            break;
                                        case "clientID":
                                        case "clientName":
                                            if (!g.equals("")) {
                                                String[] p = db.getRow(TABLE_CLIENTS_DATA, Integer.parseInt(g));

                                                n[s] = p[1];
                                            }
                                            break;
                                        case "addsID":
                                        case "addsName":
                                            if (!g.equals("")) {
                                                String[] a = db.getRow(tableAdds, Integer.parseInt(g));

                                                n[s] = a[1];
                                            }
                                            break;
                                        case "addsTreeID":
                                        case "addsTreeName":
                                            if (!g.equals("")) {
                                                String[] at = db.getRow(tableAddstree, Integer.parseInt(g));

                                                n[s] = at[1];
                                            }
                                            break;
                                        default:
                                            n[s] = g;
                                            break;

                                    }
                                } else {

                                    n[s] = g;
                                }
                            }
                        } else {
                            n[s] = "" + g;
                        }
                    }
                    model.addRow(n);

                    for (int s = 0; s < fields.length; s++) {
                        alignrender(index, s);
                        if (viewDataAS != null) {
                            String[] ss = viewDataAS[s].split("_");

                            if (ss.length > 1) {
                                if (!ss[1].equalsIgnoreCase("")) {
                                    switch (ss[1]) {

                                        case "maxWadth":

                                        case "maxWidth":
                                            index.getColumnModel().getColumn(s).setMinWidth(0);
                                            index.getColumnModel().getColumn(s).setMaxWidth(250);
                                            index.getColumnModel().getColumn(s).setPreferredWidth(250);
                                            break;

                                        case "midWadth":

                                        case "midWidth":
                                            index.getColumnModel().getColumn(s).setMinWidth(0);
                                            index.getColumnModel().getColumn(s).setMaxWidth(150);
                                            index.getColumnModel().getColumn(s).setPreferredWidth(150);
                                            break;

                                        case "minWadth":

                                        case "minWidth":

                                            index.getColumnModel().getColumn(s).setMinWidth(0);
                                            index.getColumnModel().getColumn(s).setMaxWidth(50);
                                            index.getColumnModel().getColumn(s).setPreferredWidth(50);
                                            break;
                                        case "hide":

                                            index.getColumnModel().getColumn(s).setMinWidth(0);
                                            index.getColumnModel().getColumn(s).setMaxWidth(0);
                                            index.getColumnModel().getColumn(s).setPreferredWidth(0);
                                            break;

                                    }
                                }
                            }
                        }

                    }

                }
            }

        } catch (SQLException ex) {
            ac.ui.utils.sendMessage(null, po.getWord("title.error"), ex.getMessage());

        }
        db.setCloseCon();

        index.getColumnModel().getColumn(0).setMinWidth(0);
        index.getColumnModel().getColumn(0).setMaxWidth(0);
        index.getColumnModel().getColumn(0).setPreferredWidth(0);

        model.fireTableDataChanged();
        index.repaint();
        index.repaint();
    }

    public void Indexupdate(JTable index, String query, String[] viewDataAS) {

        //    tableClearCols(index);
        DefaultTableModel model = (DefaultTableModel) index.getModel();
        String[] fields = db.getQuerygetColumsTitles(query);
        model.setColumnIdentifiers(updateArrayLang(fields));
        model.setRowCount(0);
        db.setOpenCon();
        ResultSet rs = db.getQueryWithOutCon(query);
        String test = db.retuenErrorMessage;

        try {

            if (viewDataAS == null) {

                while (rs.next()) {

                    Object[] n = new Object[fields.length];
                    for (int s = 0; s < fields.length; s++) {
                        alignrender(index, s);
                        String g = rs.getString(fields[s]);

                        n[s] = g;

                    }
                    model.addRow(n);

                }
            } else {

                while (rs.next()) {

                    Object[] n = new Object[fields.length];
                    for (int s = 0; s < fields.length; s++) {

                        String g = rs.getString(fields[s]);

                        if (fields[s].equalsIgnoreCase(CatID)) {

                            g = updateCatid(Integer.parseInt(g));
                        }

                        if (viewDataAS != null) {
                            String[] ss = viewDataAS[s].split("_");

                            if (ss.length > 0) {
                                if (!ss[0].equalsIgnoreCase("")) {

                                    switch (ss[0]) {
                                        case "productID":
                                        case "productName":
                                            if (!g.equals("")) {
                                                String[] p = db.getRow(TABLE_products, Integer.parseInt(g));

                                                n[s] = p[1];
                                            }
                                            break;
                                        case "clientID":
                                        case "clientName":
                                            if (!g.equals("")) {
                                                String[] p = db.getRow(TABLE_CLIENTS_DATA, Integer.parseInt(g));

                                                n[s] = p[1];
                                            }
                                            break;
                                        case "addsID":
                                        case "addsName":
                                            if (!g.equals("")) {
                                                String[] a = db.getRow(tableAdds, Integer.parseInt(g));

                                                n[s] = a[1];
                                            }
                                            break;
                                        case "addsTreeID":
                                        case "addsTreeName":
                                            if (!g.equals("")) {
                                                String[] at = db.getRow(tableAddstree, Integer.parseInt(g));

                                                n[s] = at[1];
                                            }
                                            break;
                                        default:
                                            n[s] = g;
                                            break;

                                    }
                                } else {

                                    n[s] = g;
                                }
                            }
                        } else {
                            n[s] = "" + g;
                        }
                    }
                    model.addRow(n);

                    for (int s = 0; s < fields.length; s++) {
                        alignrender(index, s);
                        if (viewDataAS != null) {
                            String[] ss = viewDataAS[s].split("_");

                            if (ss.length > 1) {
                                if (!ss[1].equalsIgnoreCase("")) {
                                    switch (ss[1]) {

                                        case "maxWadth":

                                        case "maxWidth":
                                            index.getColumnModel().getColumn(s).setMinWidth(0);
                                            index.getColumnModel().getColumn(s).setMaxWidth(250);
                                            index.getColumnModel().getColumn(s).setPreferredWidth(250);
                                            break;

                                        case "midWadth":

                                        case "midWidth":
                                            index.getColumnModel().getColumn(s).setMinWidth(0);
                                            index.getColumnModel().getColumn(s).setMaxWidth(150);
                                            index.getColumnModel().getColumn(s).setPreferredWidth(150);
                                            break;

                                        case "minWadth":

                                        case "minWidth":

                                            index.getColumnModel().getColumn(s).setMinWidth(0);
                                            index.getColumnModel().getColumn(s).setMaxWidth(50);
                                            index.getColumnModel().getColumn(s).setPreferredWidth(50);
                                            break;
                                        case "hide":

                                            index.getColumnModel().getColumn(s).setMinWidth(0);
                                            index.getColumnModel().getColumn(s).setMaxWidth(0);
                                            index.getColumnModel().getColumn(s).setPreferredWidth(0);
                                            break;

                                    }
                                }
                            }
                        }

                    }

                }
            }

        } catch (SQLException ex) {
            ac.ui.utils.sendMessage(null, po.getWord("title.error"), ex.getMessage());

        }
        db.setCloseCon();

        index.getColumnModel().getColumn(0).setMinWidth(0);
        index.getColumnModel().getColumn(0).setMaxWidth(0);
        index.getColumnModel().getColumn(0).setPreferredWidth(0);

        model.fireTableDataChanged();
        index.repaint();
        index.repaint();
    }

    public void Indexupdate(JTable index, String Tabel, String Field, String idTitle, String FieldTile, String Where, String indexviewAs) {
        DefaultTableModel model = (DefaultTableModel) index.getModel();
        model.setRowCount(0);
        model.setColumnIdentifiers(new String[]{idTitle, po.getWord(FieldTile)});

        db.setOpenCon();
        String w = "";

        if (!Where.equals("")) {
            w = Where + " and ";
        }

        if (withOutData) {

            w += " 1=1";
        } else {
            w += "  `rowstatus`='0' ";

        }

        ResultSet rs = db.getDataWithOutCon(Tabel, "", w);
        String test = db.retuensqlString;
        test += db.retuenErrorMessage;
        String[] Rows = new String[]{"id", Field};
        try {

            String g = "";
            while (rs.next()) {
                Object[] n = new Object[Rows.length];
                for (int i = 0; i < Rows.length; i++) {
                    if (i > 0) {
                        switch (indexviewAs) {
                            case "addsID":
                            case "addsName":
                                g = rs.getString(Rows[i]);
                                if (!g.equals("")) {

                                    try {
                                        String[] a = db.getRow(tableAdds, Integer.parseInt(g));

                                        n[i] = a[1];
                                    } catch (Exception e) {
                                    }
                                }

                                break;
                            case "addsTreeID":
                            case "addsTreeName":
                                g = rs.getString(Rows[i]);
                                if (!g.equals("")) {
                                    String[] at = db.getRow(tableAddstree, Integer.parseInt(g));

                                    n[i] = at[1];
                                }

                                break;

                            case "":
                                n[i] = rs.getString(Rows[i]);
                                break;

                            default:
                                n[i] = rs.getString(Rows[i]);
                                break;
                        }

                    } else {
                        n[i] = rs.getString(Rows[i]);

                    }
                }
                model.addRow(n);
            }
        } catch (SQLException ex) {
            ac.ui.utils.sendMessage(null, po.getWord("title.error"), ex.getMessage());

        }
        db.setCloseCon();

        index.setModel(model);
        model.fireTableDataChanged();
        index.repaint();

        index.getColumnModel().getColumn(0).setMinWidth(0);
        index.getColumnModel().getColumn(0).setMaxWidth(0);
        index.getColumnModel().getColumn(0).setPreferredWidth(0);

        alignrender(index, 1);

        /*
         DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
         rightRenderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);

         index.getColumn(1).setCellRenderer(rightRenderer);*/
    }

    public void alignrender(JTable table, int column) {

        DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
        rightRenderer.setHorizontalAlignment(JLabel.RIGHT);
        table.getColumnModel().getColumn(column).setCellRenderer(rightRenderer);

        JTableHeader jtableHeader = table.getTableHeader();
        DefaultTableCellRenderer rend = (DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer();
        rend.setHorizontalAlignment(JLabel.CENTER);
        jtableHeader.setDefaultRenderer(rend);

    }

//</editor-fold>
    public static int[] removeElement(int[] original, int element) {
        int[] n = new int[original.length - 1];
        System.arraycopy(original, 0, n, 0, element);
        System.arraycopy(original, element + 1, n, element, original.length - element - 1);
        return n;
    }

    public static String[] removeElement(String[] original, int element) {
        String[] n = new String[original.length - 1];
        System.arraycopy(original, 0, n, 0, element);
        System.arraycopy(original, element + 1, n, element, original.length - element - 1);
        return n;
    }

    public static String[] addValueToArray(String[] arr, int index, String Value) {

        String[] newArray = new String[arr.length + 1];
        int i;
        for (i = 0; i < index; i++) {
            newArray[i] = arr[i];
        }
        //  // move to position to insert new value

        newArray[i] = Value; // insert the value

        // loop until you reach the length of the old array 
        while (i < arr.length) {
            i++;
            newArray[i] = arr[i - 1];

        }

        // finally copy last value over
        //     newArray[i + 1] = arr[i];
        return newArray;
    }

}
